
class Model(object):
    
    def __init__(self):
        self.table = ""
        self.cols = ""
        self.values = []
        self.new_sql_template = "INSERT INTO {table} ({cols}) VALUES ({placeholders})"
    
    def create_new_sql(self, tableName, data):

        # 如果数据不是dict则无法解析，会抛异常
        if not isinstance(data, dict):
            raise AssertionError

        if len(data) == 0:
            raise AssertionError
        
        # 将dict的key名称作为字段名称
        self.cols = ", ".join(data.keys())

        # 根据字段值的属性生成对应的替换字符
        placeholderList = []
        for key in data.keys():
            value = data[key]
            self.values.append(value)

            if isinstance(value, str):
                placeholderList.append("'{}'")
            if isinstance(value, int):
                placeholderList.append("{:d}")
            if isinstance(value, float):
                placeholderList.append("{:.2f}")

        placeholders = ", ".join(placeholderList)
        
        sql = self.new_sql_template.format(table=tableName, cols=self.cols, placeholders=placeholders)

        return sql.format(*self.values)

